// pages/search/search.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    searchList:[],
    value:''
  },

  /**输入搜索框 */
  input_search(e){
    const{value}=e.detail;
    if(!value.trim())
    {
      this.setData({searchList:[]});
      return;
    }
    this.setData({value:e.detail.value});
     /**发送后台请求 */
     /**暂时没写 */
     /*防抖*/
    clearTimeout(this.TimeId);
    this.TimeId=setTimeout(()=>{
      this.qsearch(value);1000 });
    /** 请求返回一个searchList 这里是为了演示而静态给了一个*/
  },
  /*取消搜索*/
  cancel(e){
    this.setData({searchList:[],value:''});
    return;
  },

  async qsearch(query)
  {
    wx.showLoading({
      title: '加载中',
    })
    var that = this;
    let key = that.data.value;
    const db = wx.cloud.database();
    const _ = db.command
    db.collection('List').where(_.and([
      _.or([{
          name: db.RegExp({
          regexp: '.*' + key,
          options: 'i',
        })
      },{
          title: db.RegExp({
          regexp: '.*' + key,
          options: 'i',
        })
      },{
          content: db.RegExp({
          regexp: '.*' + key,
          options: 'i',
        })
      }
    ]),
    {isok:false}
])).get({
      success: res => {
        //console.log(res);
        const templist = res.data
        that.setData(
          {
            searchList:templist,
          }
        )
        wx.hideLoading({
          success: (res) => {},
        })
      },
      fail: err => {
        console.log(err)
      }
    })
    //console.log(that.data.searchList);
  }
})